//
//  HBReadEndController.m
//  HeartBeat
//
//  Created by karong on 2023/9/18.
//

#import "HBReadEndController.h"
#import "HBDiscoverItemNovel3Cell.h"

@interface HBReadEndController ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@end

@implementation HBReadEndController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = self.novel.guess_like;
    UINib * nib = [UINib nibWithNibName:@"HBDiscoverItemNovel3Cell" bundle:nil];
    [self.listView registerNib:nib forCellWithReuseIdentifier:@"HBDiscoverItemNovel3Cell"];
    [self layoutMainView];
    [self.listView reloadData];
}

- (void)layoutMainView {
    self.titleLabel.hidden = self.dataArray.count == 0;
}

#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count>3?3:self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBDiscoverItemNovel3Cell * bookCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemNovel3Cell" forIndexPath:indexPath];
    [bookCell setDataWithObject:self.dataArray[indexPath.row]];
    
    return bookCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = floor((HBScreenWidth - 12 - 12 - 20 - 20)/3.0f);
    size.height = size.width * 4 / 3.0f + 11 + 34;
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [ViewManager pushNovelDetailVCWithNovel:self.dataArray[indexPath.row] inController:self];
}

@end
